package com.love_weinuo.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.View;

import com.lzy.okgo.callback.StringCallback;
import com.love_weinuo.R;
import com.love_weinuo.bean.NearPindanCallBackBean;
import com.love_weinuo.databinding.ActivitySearchResultBinding;
import com.love_weinuo.ui.BaseActivity;
import com.love_weinuo.ui.adapter.NearIndexAdapter;
import com.love_weinuo.ui.adapter.OnItemClickListener;
import com.love_weinuo.util.GsonUtil;
import com.love_weinuo.util.HTTPAPI;
import com.love_weinuo.util.LogUtil;
import com.love_weinuo.util.SpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by SensYang on 2019/03/27 21:34
 */

public class SearchResultActivity extends BaseActivity {

    ActivitySearchResultBinding binding;
    List<NearPindanCallBackBean.ResultEntity.ResEntity> strs = new ArrayList<>();
    List<NearPindanCallBackBean.ResultEntity.ResEntity> pindanList = new ArrayList<NearPindanCallBackBean.ResultEntity.ResEntity>();
    NearIndexAdapter adapter;
    String keyWord;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_search_result);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        add(this);
        keyWord = getIntent().getStringExtra("module_key_word");
        binding.recycler.setLayoutManager(new LinearLayoutManager(SearchResultActivity.this));
        adapter = new NearIndexAdapter(SearchResultActivity.this, strs);
        binding.recycler.setAdapter(adapter);
        binding.recycler.setNestedScrollingEnabled(false);
        adapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                    Intent intent = new Intent(SearchResultActivity.this, PindanDetailActivity.class);
                intent.putExtra("module_pindan_bean", strs.get(position));
                intent.putExtra("module_from", "near");
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
        getNearPindan("",keyWord,"");
        binding.tvModuleSearch.setOnClickListener(this);
        binding.rlModuleBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rlModuleBack:
                finish();
                break;
            case R.id.tvModuleSearch:
                finish();
                break;
            default:
        }
    }

    String userId = (String) SpUtils.get("module_user_id", "");
    public void getNearPindan(String landId, String keyWord, String distance) {
        HTTPAPI.getInstance().getNearPindan(userId,landId,keyWord,distance, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("附近拼单获取成功" + s);
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    int errorCode = jsonObject.getInt("errcode");
                    if (errorCode == 1) {
                        binding.recycler.setVisibility(View.GONE);
                        binding.ilModuleNothing.setVisibility(View.VISIBLE);
                        showInCommonDialog("搜索暂无结果");
                        return;
                    }
                    NearPindanCallBackBean bean = GsonUtil.GsonToBean(s, NearPindanCallBackBean.class);
                    switch (bean.getErrcode()) {
                        case 0:
                            pindanList.clear();
                            pindanList.addAll(bean.getResult().getRes());
                            if (pindanList.size() == 0) {
                                if (strs.size() != 0) {
                                    showInCommonDialog("暂无更多数据");
                                    return;
                                } else {
                                    binding.recycler.setVisibility(View.GONE);
                                    binding.ilModuleNothing.setVisibility(View.VISIBLE);
                                }
                            } else {
                                binding.recycler.setVisibility(View.VISIBLE);
                                binding.ilModuleNothing.setVisibility(View.GONE);
                                strs.clear();
                                strs.addAll(pindanList);
                                adapter.notifyDataSetChanged();
                            }
                            break;
                        default:
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    LogUtil.e("json解析错误" + e.getMessage());
                }
            }

            @Override
            public void onError(Call call, Response response, Exception e) {
                LogUtil.e("附近拼单获取失败" + e.getMessage());
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        finish();
        return true;
    }
}
